home *** CD-ROM | disk | FTP | other *** search
- /*
- File: AlternateBufferDisplay.c
-
- Contains: Shows that copybits along with offscreen bitmaps
- can emulate alternating buffer displays at a reasonable rate.
-
- Written by: JW
-
- Copyright: Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 08/2000 JM Carbonized, non-Carbon code is commented out
- for demonstration purposes.
- 7/7/1999 KG Updated for Metrowerks Codewarror Pro 2.1
- 11/12/91 JW Created
-
- */
- #include "CarbonPrefix.h"
- #include <QDOffscreen.h>
- #include <Quickdraw.h>
- #include <Menus.h>
- #include <Dialogs.h>
- #include <MacTypes.h>
- #include <TextUtils.h>
- #include <Resources.h>
- #include <Errors.h>
- #include <ToolUtils.h>
-
- #define Gestalttest 0xA1AD
- #define NoTrap 0xA89F
-
- #define appleID 128
- #define appleMenu 0
- #define aboutMeCommand 1
-
- #define fileID 129
- #define quitCommand 1
-
- #define aboutMeDLOG 128
- #define okButton 1
-
- #ifndef topLeft
- #define topLeft(r) (((Point *) &(r))[0])
- #endif
-
- #ifndef botRight
- #define botRight(r) (((Point *) &(r))[1])
- #endif
-
- /*------------------------------------------------------*/
- /* Global Variables. */
- /*------------------------------------------------------*/
-
- Rect TotalRect, minRect, WinMinusScroll, InitWindowSize;
- WindowPtr myWindow;
- Boolean DoneFlag;
- MenuHandle mymenu0, mymenu1;
- GWorldPtr offscreenGWorld1, offscreenGWorld2;
-
- /*------------------------------------------------------*/
- /* showAboutMeDialog(). */
- /*------------------------------------------------------*/
-
- void showAboutMeDialog()
- {
- GrafPtr savePort;
- DialogPtr theDialog;
- short itemHit;
-
- GetPort(&savePort);
- theDialog = GetNewDialog(aboutMeDLOG, nil,(WindowPtr)(-1));
- //SetPort(theDialog);
- SetPortDialogPort(theDialog);
-
- do {
- ModalDialog(nil, &itemHit);
- } while (itemHit != okButton);
-
- //CloseDialog(theDialog);
- DisposeDialog(theDialog);
-
- SetPort(savePort);
- return;
- }
-
- /*------------------------------------------------------*/
- /* init(). */
- /*------------------------------------------------------*/
-
- void init()
- {
- RgnHandle tempRgn;
- Rect BaseRect;
- GDHandle SaveGD;
- CGrafPtr SavePort;
- QDErr myerr;
- RGBColor black = {0,0,0}, white = {65535,65535,65535};
-
- //InitGraf(&qd.thePort);
- //InitWindows();
- //InitDialogs(nil);
- InitCursor();
- FlushEvents(everyEvent, 0);
-
- /* Set up menus. */
- mymenu0 = GetMenu(appleID);
- //AppendResMenu(mymenu0, 'DRVR');
- InsertMenu(mymenu0,0);
- mymenu1 = GetMenu(fileID);
- InsertMenu(mymenu1,0);
- DrawMenuBar();
-
- /* Main initialization. */
- DoneFlag = false;
-
- /* Set Rects. */
- SetRect(&BaseRect, 40, 60, 490, 540);
- SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-56,
- BaseRect.bottom - 76);
- SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top,
- WinMinusScroll.right, WinMinusScroll.bottom);
- tempRgn =(RgnHandle)GetGrayRgn();
- HLock ((Handle) tempRgn);
- //TotalRect = (**tempRgn).rgnBBox;
- GetRegionBounds(tempRgn, &TotalRect);
- //SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40,
- // (**tempRgn).rgnBBox.bottom - 40);
- SetRect(&minRect, 80, 80, TotalRect.right - 40, TotalRect.bottom - 40);
- HUnlock ((Handle) tempRgn);
-
- /* Open window and set up picture. */
- GetGWorld (&SavePort, &SaveGD);
- myWindow = NewCWindow(nil, &BaseRect, "\p", true, zoomDocProc,
- (WindowPtr) -1, true, 150);
- SetGWorld((CGrafPtr)myWindow, SaveGD);
- DrawGrowIcon (myWindow);
-
- GetGWorld (&SavePort, &SaveGD);
-
- /* Make first offscreen buffer. */
- if (myerr=NewGWorld (&offscreenGWorld1, 0, &InitWindowSize, nil, nil, 0))
- Debugger();
- SetGWorld (offscreenGWorld1, nil);
- RGBForeColor(&white);
- PaintRect (&InitWindowSize);
-
- /* Make second offscreen buffer. */
- if (NewGWorld (&offscreenGWorld2, 0, &InitWindowSize, nil, nil, 0))
- Debugger();
- SetGWorld (offscreenGWorld2, nil);
- RGBForeColor(&black);
- PaintRect (&InitWindowSize);
-
- SetGWorld (SavePort, SaveGD);
- }
-
- /*------------------------------------------------------*/
- /* doCommand(). */
- /*------------------------------------------------------*/
-
- void doCommand(mResult)
- long mResult;
- {
- int theMenu, theItem;
- //char daName[256];
- //GrafPtr savePort;
-
- theItem = LoWord(mResult);
- theMenu = HiWord(mResult);
-
- switch (theMenu) {
- case appleID:
- if (theItem == aboutMeCommand)
- showAboutMeDialog();
- else {
- /*SetMenuItemText(mymenu0, theItem, c2pstr(daName));
- GetPort(&savePort);
- (void) OpenDeskAcc(daName);
- SetPort(savePort);*/
- }
- break;
-
- case fileID:
- switch (theItem) {
- case quitCommand:
- DoneFlag = true;
- break;
- default:
- break;
- }
- break;
- }
- HiliteMenu(0);
- return;
- }
-
- /*------------------------------------------------------*/
- /* draw(). */
- /*------------------------------------------------------*/
-
- void draw()
- {
- RGBColor black = {0,0,0}, White = {65535,65535,65535};
- long delay;
- Str255 theString;
- GDHandle screensDevice;
- Rect area;
- int i;
- GrafPtr oldPort;
- RgnHandle rgnHandle = NewRgn();
-
- GetPort(&oldPort);
- SetPortWindowPort(myWindow);
-
- RGBForeColor (&black);
- RGBBackColor (&White);
- EraseRect(&WinMinusScroll);
-
-
- area = WinMinusScroll;
-
- LocalToGlobal(&topLeft(area));
- LocalToGlobal(&botRight(area));
-
- screensDevice = GetMaxDevice(&area);
- if (screensDevice != nil) {
- //(**(**offscreenGWorld1->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
- //(**(**offscreenGWorld2->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
- (**(**(GetPortPixMap(offscreenGWorld1))).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
- (**(**(GetPortPixMap(offscreenGWorld2))).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
- }
- delay = TickCount();
- for (i=0; i< 10; i++) {
- /*CopyBits ((BitMap *) *offscreenGWorld2->portPixMap, &myWindow->portBits,
- &InitWindowSize, &WinMinusScroll, srcCopy, nil);
- CopyBits ((BitMap *) *offscreenGWorld1->portPixMap, &myWindow->portBits,
- &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
- CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld2)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)),
- &InitWindowSize, &WinMinusScroll, srcCopy, nil);
- QDFlushPortBuffer(GetWindowPort(myWindow), GetPortVisibleRegion(GetWindowPort(myWindow), rgnHandle));
- CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld1)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)),
- &InitWindowSize, &WinMinusScroll, srcCopy, nil);
- QDFlushPortBuffer(GetWindowPort(myWindow), GetPortVisibleRegion(GetWindowPort(myWindow), rgnHandle));
- }
- delay = TickCount() - delay;
- MoveTo(50,50);
- NumToString(delay, theString);
- DrawString(theString);
- QDFlushPortBuffer(GetWindowPort(myWindow), GetPortVisibleRegion(GetWindowPort(myWindow), rgnHandle));
-
- SetPort(oldPort);
- DisposeRgn(rgnHandle);
- }
-
- /*------------------------------------------------------*/
- /* main(). */
- /*------------------------------------------------------*/
-
- main()
- {
- char key;
- Boolean track;
- long growResult;
- EventRecord myEvent;
- WindowPtr whichWindow;
- int yieldTime;
- Rect tempRect1;
-
- init();
- yieldTime = 0;
- for ( ;; ) {
- if (DoneFlag)
- ExitToShell();
-
- if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
- switch (myEvent.what) {
- case mouseDown:
- switch (FindWindow(myEvent.where, &whichWindow)) {
- case inSysWindow:
- //SystemClick(&myEvent, whichWindow);
- break;
- case inMenuBar:
- doCommand(MenuSelect(myEvent.where));
- break;
- case inContent:
- break;
- case inDrag:
- DragWindow (whichWindow, myEvent.where, &TotalRect);
- draw();
- DrawGrowIcon (whichWindow);
- break;
- case inGrow:
- growResult = GrowWindow (whichWindow, myEvent.where,
- &minRect);
- SizeWindow(whichWindow, LoWord(growResult),
- HiWord(growResult), true);
- //EraseRect(&whichWindow->portRect);
- EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
- /*SetRect(&WinMinusScroll, whichWindow->portRect.left,
- whichWindow->portRect.top,
- whichWindow->portRect.right-20,
- whichWindow->portRect.bottom - 20);*/
- SetRect(&WinMinusScroll, tempRect1.left,
- tempRect1.top,
- tempRect1.right-20,
- tempRect1.bottom - 20);
- draw();
- DrawGrowIcon (whichWindow);
- break;
- case inGoAway:
- track = TrackGoAway (whichWindow, myEvent.where);
- if (track) {
- //CloseWindow (whichWindow);
- DoneFlag = true;
- }
- break;
- case inZoomIn:
- track = TrackBox (whichWindow, myEvent.where, inZoomIn);
- if (track) {
- ZoomWindow (whichWindow, inZoomIn, true);
- //EraseRect(&whichWindow->portRect);
- EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
- /*SetRect(&WinMinusScroll, whichWindow->portRect.left,
- whichWindow->portRect.top,
- whichWindow->portRect.right-20,
- whichWindow->portRect.bottom - 20);*/
- SetRect(&WinMinusScroll, tempRect1.left,
- tempRect1.top,
- tempRect1.right-20,
- tempRect1.bottom - 20);
- draw();
- DrawGrowIcon (whichWindow);
- }
- break;
- case inZoomOut:
- track = TrackBox (whichWindow, myEvent.where, inZoomOut);
- if (track) {
- ZoomWindow (whichWindow, inZoomOut, true);
- //EraseRect(&whichWindow->portRect);
- EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
- //SetRect(&WinMinusScroll, whichWindow->portRect.left,
- // whichWindow->portRect.top,
- // whichWindow->portRect.right-20,
- // whichWindow->portRect.bottom - 20);
- SetRect(&WinMinusScroll, tempRect1.left,
- tempRect1.top,
- tempRect1.right-20,
- tempRect1.bottom - 20);
- draw();
- DrawGrowIcon (whichWindow);
- }
- break;
- default:
- break;
- }
- break;
- case keyDown:
- case autoKey:
- key = myEvent.message & charCodeMask;
- if ( myEvent.modifiers & cmdKey )
- if ( myEvent.what == keyDown )
- doCommand(MenuKey(key));
- break;
- case updateEvt:
- if ((WindowPtr) myEvent.message == myWindow) {
- BeginUpdate((WindowPtr) myWindow);
- draw();
- DrawGrowIcon (whichWindow);
- EndUpdate((WindowPtr) myWindow);
- }
- break;
- case diskEvt:
- break;
- case activateEvt:
- if (myEvent.modifiers & activeFlag) {
- draw();
- DrawGrowIcon(myWindow);
- }
- case app4Evt:
- if ((myEvent.message << 31) == 0) {
- yieldTime = 30;
- }
- else {
- yieldTime = 0;
- //SetPort((WindowPtr) myWindow);
- SetPortWindowPort(myWindow);
- }
- break;
- default:
- break;
- }
- }
- }
- }